//
//  SubscriptionViewController.m
//  ZAKER
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SubscriptionViewController.h"


@interface SubscriptionViewController ()

@property (nonatomic, retain) NSMutableArray *imageArray; // 存放轮播图的照片
@property (nonatomic, retain) NSMutableArray *urlArray; // 存放详情网址,传到下一页
@property (nonatomic, retain) SubscriptionModel *webModel;
@property (nonatomic, retain) ImageScrollView *scroll;
@property (nonatomic, retain) UIActivityIndicatorView *activity;

@end


@implementation SubscriptionViewController

-(void)dealloc
{
    
    [_webModel release];
    [_activity release];
    [_imageArray release];
    [_urlArray release];
    [_labelArray release];
    [_scroll release];
    
    [super dealloc];
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        self.imageArray = [NSMutableArray array];
        self.urlArray = [NSMutableArray array];
        self.labelArray = [NSMutableArray array]; // 获取详情页面的title
        
    }
    
    return self;
    
}
- (void)viewDidLoad
{
    
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = RGBACOLOR(240, 51, 51, 1);
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 + 37.5 * myWidth, 10 * myHeight, 75 * myWidth, 40 * myHeight)];
    label.text = @"订阅";
    label.textAlignment = 1;
    self.navigationItem.titleView = label;
    [label release];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        
        if (_activity != nil) {
            [_activity removeFromSuperview];
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"数据加载失败" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
            [alert release];
            
        }
    });
    
    [self createScroll];
    [self myConnection];
    [self createButton];
    
    
}


#pragma mark - 数据解析
-(void)myConnection
{

    [MyAFNetworking GetWithURL:LunBoTu dic:nil data:^(id responsder) {
    
        NSDictionary *dic = [responsder objectAtIndex:1];
        NSArray *myarr = [dic objectForKey:@"item"];
        
        for (NSDictionary *item in myarr) {
            SubscriptionModel *model = [[SubscriptionModel alloc] init];
            model.promotion_img = [item objectForKey:@"thumbnail"];
            model.title = [item objectForKey:@"title"];
            model.webUrl = [item objectForKey:@"commentsUrl"];
            
            [self.imageArray addObject:model];
            [model release];
           
        }
        
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:_imageArray.lastObject];
        
        for (int i = 0; i < [_imageArray count]; i++) {
            [arr addObject:_imageArray[i]];
        }
        
        [arr addObject:_imageArray.firstObject];
         [_scroll setImageWithModels:arr];
        _activity = nil;
        
    }];
}

#pragma mark - 创建九个button

-(void)createButton
{
    NSArray *imageArray = [NSArray arrayWithObjects:@"1.png",  @"3.png", @"2.png",@"4.png", @"5.png", @"6.png", @"8.png",@"7.png", @"9.png", nil];
    NSArray *nameArray = [NSArray arrayWithObjects:@"娱乐八卦", @"商业圈",@"体育频道", @"时尚先生网", @"军事",@"财经新闻",  @"名车志",@"科技",  @"旅游频道", nil];
    
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200 * myHeight, self.view.frame.size.width, (self.view.frame.size.height - 200) * myHeight - 49)];
    [backImage setImage:[UIImage imageNamed:@"blur.jpg"]];
    [self.view addSubview:backImage];
    backImage.userInteractionEnabled = YES;
    [backImage release];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
    effectview.frame = CGRectMake(0, 0, 375 * myWidth, 667 * myHeight);
    effectview.alpha = 0.85f;
    [backImage addSubview:effectview];
    [effectview release];
    
    CGFloat offY = 0;
    CGFloat heightButton = (self.view.frame.size.height - self.scroll.frame.size.height - 49 - 64) / 3;
    
    for (int i = 0; i < 9; i++) {
        
        if (i % 3 == 0 && i != 0) {
            
            offY += heightButton;
            
        }
        
        CustonButton *button = [[CustonButton alloc]initWithFrame:CGRectMake(i % 3 * 125 * myWidth, offY, 125 * myWidth, heightButton)];
        [backImage addSubview:button];
        [button release];
        button.tag = i + 1;
        button.myImage.userInteractionEnabled = NO;
        button.layer.borderColor = [UIColor grayColor].CGColor;
        button.layer.borderWidth = 0.3;
        button.myImage.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
        
        NSString *str = [nameArray objectAtIndex:i];
        button.myLabel.text = str;
        [self.labelArray addObject:str];
        
        [button addTarget:self action:@selector(priceAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
}


#pragma mark - 创建轮播图的scrollView

-(void)createScroll
{
    
    self.scroll = [[[ImageScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * myHeight)] autorelease];
    [self.view addSubview:_scroll];
    _scroll.target = self;
    _scroll.action = @selector(scrollAction:);
    
    self.activity = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
    [_scroll addSubview:_activity];
    _activity.frame = CGRectMake(self.scroll.frame.size.width / 2 - 15 * myWidth, self.scroll.frame.size.height / 2 - 15* myHeight, 30 * myWidth, 30 * myHeight);
    [_activity startAnimating];
//    [_scroll release];
//    [_activity release];
//    
}

#pragma mark - 点击事件

#pragma mark ** 九个button的点击事件

-(void)priceAction:(id)sender
{
    
    UIButton *button = (UIButton *)sender;
    
    SubDetailViewController *detail = [[SubDetailViewController alloc]init];
    [detail sendTag:button.tag labelArray:self.labelArray];
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}

#pragma mark ** 轮播图点击进入详情

-(void)scrollAction:(MyImageView *)image
{
    
    ImageWebViewController *web = [[ImageWebViewController alloc]init];
    web.url = [NSURL URLWithString:image.model.webUrl];
    [self.navigationController pushViewController:web animated:YES];
    [web release];
    
}

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
    
}


@end
